package com.qzsoft.zhjd.server.modules.ludong.contaminant.controller;

import com.qzsoft.zhjd.server.common.utils.MapToBeanUtils;
import com.qzsoft.zhjd.server.common.utils.R;
import com.qzsoft.zhjd.server.modules.ludong.contaminant.entity.ContaminantMonitorDetailEntity;
import com.qzsoft.zhjd.server.modules.ludong.contaminant.entity.ContaminantMonitorEntity;
//import cn.qzsoft.collect.server.modules.ludong.contaminant.entity.EnvDataEntity;
import com.qzsoft.zhjd.server.modules.ludong.contaminant.service.ContaminantMonitorService;
//import cn.qzsoft.collect.server.modules.ludong.contaminant.service.EnvWarningFlagService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 *
 *功能描述：环境监测
 * @author jiangyafei
 * @email jiangyf_sd@163.com
 * @date 2020-06-10 17:35:08
 */
@Slf4j
@RestController
@RequestMapping("datacollect/contaminant/contaminantmonitor")
public class ContaminantMonitorController {

    @Autowired
    private ContaminantMonitorService contaminantMonitorService;
//    @Autowired
//    private EnvWarningFlagService envWarningFlagService;


    /**
     * 功能描述: 环境监控
     * @param params 参数
     * @return : cn.qzsoft.common.utils.R
     * @author : liugf
     * @date : 2020/6/10 19:45
     */
    @PostMapping("/save")
    public R save(@RequestParam Map<String,Object> params)throws Exception{
        try{
            log.info("环境监测===="+params.toString());
            //监控详细数据
            Gson gson=new Gson();
            List<Map<String,Object>> dataDetail = gson.fromJson(params.get("data_detail").toString(), new TypeToken<List<Map<String,Object>>>(){}.getType());
            params.remove("data_detail");
            Map<String,Object> map= MapToBeanUtils.toReplaceKeyLow(params);
            ContaminantMonitorEntity contaminantMonitorEntity=MapToBeanUtils.mapToObject(map,ContaminantMonitorEntity.class);
            if (StringUtils.isNotBlank(contaminantMonitorEntity.getId())){
                List<ContaminantMonitorDetailEntity> listDetail=new ArrayList<>();
                for (Map<String,Object> entity:dataDetail){
                    entity=MapToBeanUtils.toReplaceKeyLow(entity);
                    ContaminantMonitorDetailEntity detailEntity=MapToBeanUtils.mapToObject(entity,ContaminantMonitorDetailEntity.class);
                    listDetail.add(detailEntity);
                }
                contaminantMonitorService.insertMonitorAndDetailData(contaminantMonitorEntity,listDetail);
//                envWarningFlagService.dataCollection(contaminantMonitorEntity,listDetail);
                return R.ok();
            }
            return R.error(400,"数据为空或id为空！");
        }catch (Exception ex){
            ex.printStackTrace();
            log.error(ex.getMessage());
            return R.error(400,ex.getMessage());
        }

    }


}
